// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <utility>
#include <cantor/parser/parser.h>
#include <cantor/transform/ctors/udf_arg.h>
#include <cantor/transform/ctors/dict_arg.h>
#include <cantor/transform/ctors/expr_list_arg.h>
#include <cantor/transform/expr.h>
#include <cantor/transform/ctors/map_arg.h>
#include <cantor/transform/clause/type_name_uri.h>

namespace cantor::transform {
    void TransUdfArg::resolve_ast(const cantor::parser::UdfArg *node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        if (node->uri) {
            status = turbo::data_loss_error("no uri");
            return;
        }
        if (node->arg_type != pb::ast::ENT_DICT_ARG && node->arg_type != pb::ast::ENT_MAP_ARG && node->arg_type !=
            pb::ast::ENT_EXPR_LIST_ARG) {
            status = turbo::data_loss_error("arg type mismatch");
            return;
        }

        if (node->arg) {
            if (node->arg_type != node->arg->expr_type) {
                status = turbo::data_loss_error("arg type mismatch");
                return;
            }
            switch (node->arg_type) {
                case pb::ast::ENT_DICT_ARG:
                    status = TransDictArg::resolve_to(static_cast<const parser::DictArg *>(node->arg),
                                                      udf_arg.mutable_dict_arg());
                    break;
                case pb::ast::ENT_MAP_ARG:
                    status = TransMapArg::resolve_to(static_cast<const parser::MapArg *>(node->arg),
                                                     udf_arg.mutable_map_arg());
                    break;
                case pb::ast::ENT_EXPR_LIST_ARG:
                    status = TransExprListArg::resolve_to(static_cast<const parser::ExprListArg *>(node->arg),
                                                          udf_arg.mutable_expr_list_arg());
                    break;
                default:
                    status = turbo::data_loss_error("arg type mismatch");
            }
            if (!status.ok()) {
                return;
            }
        }

        status = TransTypeNameUri::resolve_to(node->uri, udf_arg.mutable_uri());
        if (!status.ok()) {
            return;
        }
        udf_arg.set_arg_type(node->arg_type);

        set_proto_location(node, udf_arg.mutable_location());
        set_proto_status(udf_arg.mutable_status());
    }

    const turbo::Status &TransUdfArg::check() {
        if (!status.ok()) {
            return status;
        }
        return status;
    }

    turbo::Status TransUdfArg::resolve_to(turbo::Nonnull<const cantor::parser::UdfArg *> node,
                                          turbo::Nonnull<pb::ast::UdfArg *> out) {
        TransUdfArg translator;
        translator.resolve_ast(node);
        auto status = translator.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(translator.udf_arg);
        return status;
    }
} // namespace cantor::transform
